package com.zmovie.front.controller;

import com.alibaba.fastjson.JSON;
import com.zmovie.common.utils.FileUtils;
import com.zmovie.framework.domain.*;
import com.zmovie.framework.domain.dto.ActorGroupDTO;
import com.zmovie.framework.domain.dto.ActorGroupItemDto;
import com.zmovie.framework.service.*;
import com.zmovie.front.constant.Constants;
import com.zmovie.front.controller.vo.MovieVO;
import com.zmovie.front.util.MoviePathUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec com.zmovie.front.controller
 * @since 2023-12-15 12:37
 */
@Controller
@RequestMapping("/movie")
@Api(tags = "电影详情")
public class MovieInfoController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private MovieRateService movieRateService;
    @Autowired
    private MovieToTypeService movieToTypeService;
    @Autowired
    private MovieAreaService movieAreaService;
    @Autowired
    private ActorMovieService actorMovieService;

    @GetMapping("/{id}")
    @ApiOperation("电影详情页面")
    public String info(Model model, @PathVariable("id") Integer id){
        Movie movie = movieService.getById(id);
        model.addAttribute(Constants.MOVIE_KEY, convert(movie));
        return "movie_info";
    }
    private MovieVO convert(Movie movie){
        //处理文件路径
        MovieVO movieVO = MoviePathUtil.dealMovieFilePath(movie);
        //评分
        MovieRate movieRate = movieRateService.getByMovieId(movie.getId());
        movieVO.setMovieRate(movieRate);
        //类型
        List<MovieType> types = movieToTypeService.listTypeByMovieId(movie.getId());
        movieVO.setTypes(types);
        //地区
        MovieArea area = movieAreaService.getById(movie.getAreaId());
        movieVO.setMovieArea(area);
        //人员
        List<ActorGroupDTO> actorGroup = actorMovieService.listActorByMovieIdGroupByRole(movie.getId());
        actorGroup = actorGroup.stream().peek(actorGroupDTO -> {
            //处理图片路径
            List<ActorGroupItemDto> list = actorGroupDTO.getActors().stream().peek(e -> {
                List<String> pictures = JSON.parseArray(e.getPicture(), String.class);
                e.setPicture(FileUtils.dealFilePath(pictures.get(0)));
            }).collect(Collectors.toList());
            actorGroupDTO.setActors(list);
        }).collect(Collectors.toList());
        movieVO.setActorGroup(actorGroup);
        return movieVO;
    }
}
